package cn.dzhang.rcp.hyperbola.views;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import cn.dzhang.rcp.hyperbola.HyperbolaContentProvider;
import cn.dzhang.rcp.hyperbola.HyperbolaLabelProvider;
import cn.dzhang.rcp.hyperbola.common.UIConstants;
import cn.dzhang.rcp.hyperbola.common.UIUtils;
import cn.dzhang.rcp.hyperbola.model.Contact;
import cn.dzhang.rcp.hyperbola.model.ContactAdapater;
import cn.dzhang.rcp.hyperbola.model.ContactEntry;
import cn.dzhang.rcp.hyperbola.model.ContactGroup;
import cn.dzhang.rcp.hyperbola.model.Session;

public class ContactView extends ViewPart {

	public static final String ID = "cn.dzhang.rcp.hyperbola.views.ContactView"; //$NON-NLS-1$
	private Session session;
	private TreeViewer tvContact;

	public ContactView() {
	}

	/**
	 * Create contents of the view part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.BORDER);
		container.setLayout(new FillLayout());
		initSession();
		tvContact = new TreeViewer(container, SWT.MULTI);
		tvContact.setAutoExpandLevel(3);
		getSite().setSelectionProvider(tvContact);

		tvContact.setLabelProvider(new HyperbolaLabelProvider());
		tvContact.setContentProvider(new HyperbolaContentProvider());
		tvContact.setInput(session.getRoot());

		createActions();
		initializeToolBar();
		initializeMenu();
		initializeListeners();
	}

	private void initializeListeners() {
		session.getRoot().addContactListener(new ContactAdapater() {
			@Override
			public void contactChanged(ContactGroup group, Contact entry) {
				tvContact.refresh();
			}
		});

		tvContact.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event
						.getSelection();
				if (!selection.isEmpty()) {
					Contact contact = (Contact) selection.getFirstElement();
					setMessage(UIConstants.Images.ONLINE, contact.getName());
				}
			}
		});
	}

	private void setMessage(Image image, String message) {
		UIUtils.setMessage(getViewSite(), image, message);
	}

	private void initSession() {
		session = new Session();
		ContactGroup root = session.getRoot();

		ContactGroup friendsGroup = new ContactGroup(root, "Friends");
		friendsGroup.addEntry(new ContactEntry(friendsGroup, "Alize", "aliz",
				"localhost"));
		friendsGroup.addEntry(new ContactEntry(friendsGroup, "Jing Guo",
				"Jing GeGe", "localhost"));

		root.addEntry(friendsGroup);

		ContactGroup otherGroup = new ContactGroup(root, "Other");
		otherGroup.addEntry(new ContactEntry(otherGroup, "Huang YaoShi",
				"TaoHua island", "localhost"));
		root.addEntry(otherGroup);

	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		// IToolBarManager toolbarManager = getViewSite().getActionBars()
		// .getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		// IMenuManager menuManager = getViewSite().getActionBars()
		// .getMenuManager();
	}

	@Override
	public void setFocus() {
		tvContact.getControl().setFocus();
	}

	@Override
	public void dispose() {
		super.dispose();
	}

}
